//
//  UserDataView.swift
//  IdleFish
//
//  Created by Ben on 2021/12/14.
//

import SwiftUI
import SDWebImageSwiftUI

// 添加Int，这样通过rawValue取值即可
enum UserDataAction: Int, CaseIterable {
    case handle
    case account
    case nickName
    case gender
    case birthday
    case address
    case introduceBriefly
    case industry
    case position
    case school
    case loved
    
    
    var title: String {
        switch self {
        case .handle:
            return "头像"
        case .account:
            return "会员名"
        case .nickName:
            return "昵称"
        case .gender:
            return "性别"
        case .birthday:
            return "生日"
        case .address:
            return "常住"
        case .introduceBriefly:
            return "简介"
        case .industry:
            return "行业"
        case .position:
            return "职位名称"
        case .school:
            return "院校名称"
        case .loved:
            return "喜欢的"
        }
    }
    
}

struct UserDataView: View {
    
    // 用来控制退出当前view
    @Environment(\.presentationMode) private var presentationModel
    // initVM，从中获取用户信息
    @EnvironmentObject var initVM: InitViewModel
    
    @StateObject var userDataVM: UserDataViewModel = UserDataViewModel()
    
    private let userDataActionList: [UserDataAction] = UserDataAction.allCases
    
    var body: some View {
        VStack {
            UserDataNavView()
            
            UserDataFormView()
        }
        .overlay {
            SetUserGenderView().environmentObject(userDataVM)
                .offset(y: userDataVM.showGenderPicker ? 0 : SCREEN_HEIGHT)
        }
        .overlay {
            SetUserBirthdayView().environmentObject(userDataVM)
                .offset(y: userDataVM.showBrithdayPicker ? 0 : SCREEN_HEIGHT)
        }
        .confirmationDialog("请选择照片来源", isPresented: $userDataVM.showSelectImageType, titleVisibility: .visible) {
            Button("相册") {
                userDataVM.photoSource = .photoLibrary
                userDataVM.showImagePicker.toggle()
            }
            
            Button("拍照") {
                userDataVM.photoSource = .camera
                userDataVM.showImagePicker.toggle()
                
            }
        }
        .fullScreenCover(isPresented: $userDataVM.showImagePicker) {
            ImagePicer(sourceType: userDataVM.photoSource) { selectedImage in
                Dlog(selectedImage)
                
                userDataVM.selectedHandleImage = selectedImage
            }
        }
        .onAppear(perform: {
            userDataVM.initVM = initVM
        })
        .navigationBarHidden(true)
    }
    
    private func UserDataFormView() -> some View {
        ScrollView {
            
            Text("基本信息")
                .foregroundColor(Color(hex: 0x9C9D9D))
                .font(.system(size: 12, weight: .semibold))
                .frame(maxWidth: .infinity, alignment: .leading)
                .padding(.leading, 16)
                .padding(.top, 20)
            
            
            VStack(spacing: 0) {
                ForEach(userDataActionList, id: \.self) { userDataAction in
                    switch userDataAction {
                    case .handle:
                        UserDataHandleCell(userDataAction: userDataAction)
                            .contentShape(Rectangle())
                            .onTapGesture {
                                Dlog("更换头像，time:\(Date().timeIntervalSince1970)")
                                userDataVM.showSelectImageType.toggle()
                            }
                    case .account:
                        UserDataAccountCell(userDataAction: userDataAction)
                    case .loved:
                        EmptyView()
                    default:
                        
                        // 点击昵称push出新的界面，在这个界面中展示当前昵称并可以输入新昵称进行修改
                        UserDataDefaultCell(userDataAction: userDataAction)
                        // 同样要扩展点击区域
                            .contentShape(Rectangle())
                            .onTapGesture {
                                Dlog(userDataAction)
                                
                                switch userDataAction {
                                    // 修改昵称
                                case .nickName:
                                    userDataVM.pushSetNickNameView.toggle()
                                    // 修改性别
                                case .gender:
                                    withAnimation {
                                        userDataVM.showGenderPicker.toggle()
                                    }
                                case .birthday:
                                    withAnimation {
                                        userDataVM.showBrithdayPicker.toggle()
                                    }
                                default:
                                    ()
                                }
                            }
                    }
                }
            }
            
            NavigationLink(destination: SetNickNameView().environmentObject(userDataVM), isActive: $userDataVM.pushSetNickNameView) {
                
            }
        }
    }
    
    private func UserDataNavView() -> some View {
        CustomNavView {
            VStack(spacing: 2) {
                Text("个人资料设置")
                    .font(.system(size: 16, weight: .semibold))
                Text("已完成10%")
                    .font(.system(size: 10))
                    .foregroundColor(Color.black.opacity(0.5))
            }
        } backAction: {
            presentationModel.wrappedValue.dismiss()
        }
    }
    
    private func UserDataDefaultCell(userDataAction: UserDataAction) -> some View {
        HStack(spacing: 6) {
            Text(userDataAction.title)
                .fontWeight(.semibold)
            
            Spacer()
            
            Text(initVM.userInfoDefaultValueInUserData(userDataAction: userDataAction) as! String)
                .foregroundColor(Color(hex: 0x8F8F8F))
            
            Image(systemName: "chevron.right")
                .foregroundColor(Color(hex: 0xE6E5E6))
            
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 20)
        
    }
    
    private func UserDataAccountCell(userDataAction: UserDataAction) -> some View {
        HStack(spacing: 6) {
            Text(userDataAction.title)
                .fontWeight(.semibold)
            
            Spacer()
            
            Text(initVM.userInfoDefaultValueInUserData(userDataAction: userDataAction) as! String)
                .foregroundColor(Color(hex: 0x8F8F8F))
            
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 20)
        
    }
    
    private func UserDataHandleCell(userDataAction: UserDataAction) -> some View {
        HStack(spacing: 6) {
            Text(userDataAction.title)
                .fontWeight(.semibold)
            
            Spacer()
            
            // 本地更新完成
            AsyncImage(url: URL(string: initVM.userInfoDefaultValueInUserData(userDataAction: userDataAction) as? String ?? "")) { img in
                img.resize(size: CGSize(width: 60, height: 60), contentModel: .fill)
                    .clipShape(Circle())
            } placeholder: {
                Image("user_handle_placholder")
                    .resize(size: CGSize(width: 60, height: 60), contentModel: .fill)
            }
            
            
            
            Image(systemName: "chevron.right")
                .foregroundColor(Color(hex: 0xE6E5E6))
            
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 20)
    }
    
}

//struct UserDataView_Previews: PreviewProvider {
//    static var previews: some View {
//        UserDataView()
//    }
//}
